package com.bodu.system.controller;

import com.bodu.core.common.AbstractCommonController;
import com.bodu.core.entity.ParamReq;
import com.bodu.core.entity.result.PageResult;
import com.bodu.core.entity.result.Result;
import com.bodu.global.annon.TraceLog;
import com.bodu.global.security.LoginUserUtil;
import com.bodu.system.entity.request.DicItemRequest;
import com.bodu.system.entity.vo.DicItemResVo;
import com.bodu.system.entity.vo.DicItemVo;
import com.bodu.system.service.DicItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* 字典项 API
* @author Administrator
*/
@Api(tags = "字典项 API")
@RestController
@RequestMapping("/dicItem")
public class DicItemController extends AbstractCommonController<DicItemService, DicItemRequest, DicItemVo>{

    private final DicItemService dicItemService;

    public DicItemController(DicItemService dicItemService) {
        this.dicItemService = dicItemService;
    }

    @TraceLog()
    @ApiOperation("详情")
    @GetMapping("{id}")
    public Result getModel(@PathVariable("id") Long id) {
        return super.getModel(dicItemService, id);
    }

    @TraceLog()
    @PostMapping
    @ApiOperation("保存字典项")
    public Result<Integer> save(@RequestBody DicItemRequest dicItemRequest) {
        return super.save(dicItemService, dicItemRequest);
    }

    @TraceLog()
    @PutMapping
    @ApiOperation("更新字典项")
    public Result<Integer> update(@RequestBody DicItemRequest dicItemRequest) {
        return super.update(dicItemService, dicItemRequest);
    }

    @TraceLog()
    @PostMapping("listPage")
    @ApiOperation("字典项-查询")
    public Result<List<DicItemVo>> listPage(@RequestBody ParamReq<DicItemRequest> req)  {
        return super.listPage(dicItemService, req);
    }

    @TraceLog()
    @GetMapping("listDicItem/{dicCode}")
    @ApiModelProperty("字典可用项")
    public Result<List<DicItemResVo>> listDicItem(@PathVariable("dicCode") String dicCode) {
        try {
            List<DicItemResVo> list = dicItemService.listDicItem(dicCode);
            return Result.success(list);
        } catch (Exception e) {
            return Result.fail("查询失败！");
        }
    }

    @TraceLog()
    @PostMapping("countPage")
    @ApiOperation("字典项-统计")
    public Result<Integer> countPage(@RequestBody ParamReq<DicItemRequest> req)  {
        return super.countPage(dicItemService, req);
    }

    @PostMapping("page")
    @ApiOperation("字典项-分页数据")
    public Result<PageResult> page(@RequestBody ParamReq<DicItemRequest> req)  {
        return super.page(dicItemService, req);
    }

    @TraceLog()
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除字典项")
    public Result<Integer> delete(@PathVariable("id") Long id) {
        return super.delete(dicItemService, id);
    }

    @TraceLog()
    @ApiOperation("批量删除字典项")
    @DeleteMapping("{ids}")
    public Result<Integer> batchDelete(@PathVariable("ids") List<Long> ids) {
        return super.batchDelete(dicItemService, ids);
    }

    @Override
    public Long getLoginUserId() {
        return LoginUserUtil.getUserId();
    }
}
